clc;clear;close all;
%==========================Phi====================================
std = 'Data/STD/Phi64.mat';
Phi = load(std);Phi = Phi.Phi;
% fig = figure;
% imshow(mat2gray(Phi));
% cb = colorbar;
% cb.Limits = [-pi,pi];
% cb.Label.String = 'Phase(rad)';
% cb.Label.FontName = 'Times New Roman';
% cb.Label.FontSize = 15;
% colormap('parula'); % 使用 jet 颜色映射
% title("Reference",'FontName','Times New Roman','FontSize',15);
% saveas(fig, 'Data/STD/Phi.fig');
% saveas(fig, 'Data/STD/Phi.bmp', 'bmp');
%==========================unwarpphase====================================
std = 'Data/STD/unwarpphase64.mat';
unwarpphase = load(std);unwarpphase = unwarpphase.unwarpphase;
% fig = figure;
% imshow(mat2gray(unwarpphase));
% line([1, 500],[250, 250],'LineWidth',2,'Color','r');
% cb = colorbar;
% cb.Limits = [0,1];
% cb.Label.String = 'Phase(rad)';
% cb.Label.FontSize = 15;
% cb.Label.FontName = 'Times New Roman';
% colormap('parula'); % 使用 jet 颜色映射
% title("Reference",'FontName','Times New Roman','FontSize',15);
% saveas(fig, 'Data/STD/unwarpphase64.fig');
% saveas(fig, 'Data/STD/unwarpphase64.bmp', 'bmp');
%% ========================== Experiment 1  =================================
experimentName = 'Experiment (3)';
group = 8;
RMSE = zeros(4,group);
TIME = zeros(6,group);
X = linspace(25, 95, group);
name = 'F';
for g = 1:group
    dataName = ['Data (',int2str(g),')'];
    %================================TIME===================================
%     time = load(['Data/', experimentName, '/', dataName,'/TIME.mat']);
%     TIME(1:5,g) = time.TIME;
%     wls_estimation_time = load(['Data/', experimentName, '/', dataName,'/WLS/time.mat']);
%     TIME(6,g) = wls_estimation_time.time;
    %================================RMSE===================================
    if strcmp(name,'F')
        algorithmsName = 'LS';
        LS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase',int2str(X(g)),'.mat']);
        RMSE(1,g) = LS.RMSE_unwarpphase;
        algorithmsName = 'WLS';
        WLS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase',int2str(X(g)),'.mat']);
        RMSE(2,g) = WLS.RMSE_unwarpphase;
        algorithmsName = 'WLS(Zhang)';
        WLS_Zhang = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase',int2str(X(g)),'.mat']);
        RMSE(3,g) = WLS_Zhang.RMSE_unwarpphase;
        algorithmsName = 'WPCA';
        WPCA = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase',int2str(X(g)),'.mat']);
        RMSE(4,g) = WPCA.RMSE_unwarpphase;
    else
        algorithmsName = 'LS';
        LS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase64.mat']);
        RMSE(1,g) = LS.RMSE_unwarpphase;
        algorithmsName = 'WLS';
        WLS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase64.mat']);
        RMSE(2,g) = WLS.RMSE_unwarpphase;
        algorithmsName = 'WLS(Zhang)';
        WLS_Zhang = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase64.mat']);
        RMSE(3,g) = WLS_Zhang.RMSE_unwarpphase;
        algorithmsName = 'WPCA';
        WPCA = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/RMSE_unwarpphase64.mat']);
        RMSE(4,g) = WPCA.RMSE_unwarpphase;
    end
    %===============================Phi======================================
%     algorithmsName = 'LS';
%     LS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/Phi.mat']);
%     LS_Phi = LS.Phi;
%     algorithmsName = 'WLS';
%     WLS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/Phi.mat']);
%     WLS_Phi = WLS.Phi;
%     algorithmsName = 'WLS(Zhang)';
%     WLS_Zhang = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/Phi.mat']);
%     WLS_Zhang_Phi = WLS_Zhang.Phi;
%     algorithmsName = 'WPCA';
%     WPCA = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/Phi.mat']);
%     WPCA_Phi = WPCA.Phi;
%     fig = figure;
%     tlayout = tiledlayout(1,5);
%     nexttile;imshow(mat2gray(LS_Phi));title('LS','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WLS_Phi));title('Proposed Method','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WLS_Zhang_Phi));title('Zhang','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WPCA_Phi));title('WPCA','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(Phi));title('Reference','FontName','Times New Roman','FontSize',12);
%     set(gcf,'Position',[500,500,1200,300]); 
%     cb = colorbar;
%     cb.Layout.Tile = 'east';
%     cb.Label.String = 'Phase(rad)';
%     cb.Label.FontSize = 12;
%     cb.Label.FontName = 'Times New Roman';
%     cb.Limits = [-pi pi];
%     colormap('parula'); % 使用 jet 颜色映射
%     tlayout.TileSpacing = 'compact';
%     tlayout.Padding = 'compact';
%     saveas(fig,['Data/',experimentName,'/Data (',int2str(g),')/Phi.fig']);
%     saveas(fig, ['Data/',experimentName,'/Data (',int2str(g),')/Phi.bmp'], 'bmp');
%     saveas(fig, ['Data/',experimentName,'/Data (',int2str(g),')/Phi.jpg'], 'jpg');
    %============================unwarpphase=================================
    if strcmp(name,'F')
        std = ['Data/STD/unwarpphase',int2str(X(g)),'.mat'];
        unwarpphase = load(std);unwarpphase = unwarpphase.unwarpphase;
    else
        std = 'Data/STD/unwarpphase64.mat';
        unwarpphase = load(std);unwarpphase = unwarpphase.unwarpphase;
    end
    
    algorithmsName = 'LS';
    LS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/unwarpphase.mat']);
    LS_unwarpphase = LS.unwarpphase;
    algorithmsName = 'WLS';
    WLS = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/unwarpphase.mat']);
    WLS_unwarpphase = WLS.unwarpphase;
    algorithmsName = 'WLS(Zhang)';
    WLS_Zhang = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/unwarpphase.mat']);
    WLS_Zhang_unwarpphase = WLS_Zhang.unwarpphase;
    algorithmsName = 'WPCA';
    WPCA = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/unwarpphase.mat']);
    WPCA_unwarpphase = WPCA.unwarpphase;
    %=============================unwarpphase================================
%     [I_rows, I_cols] = size(unwarpphase);
%     fig = figure;
%     tlayout = tiledlayout(1,5);
%     nexttile;imshow(mat2gray(LS_unwarpphase));line([1, I_cols],[I_rows/2, I_rows/2],'LineWidth',2,'Color','r');title('LS','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WLS_unwarpphase));line([1, I_cols],[I_rows/2, I_rows/2],'LineWidth',2,'Color','r');title('Proposed Method','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WLS_Zhang_unwarpphase));line([1, I_cols],[I_rows/2, I_rows/2],'LineWidth',2,'Color','r');title('Zhang','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(WPCA_unwarpphase));line([1, I_cols],[I_rows/2, I_rows/2],'LineWidth',2,'Color','r');title('WPCA','FontName','Times New Roman','FontSize',12);
%     nexttile;imshow(mat2gray(unwarpphase));line([1, I_cols],[I_rows/2, I_rows/2],'LineWidth',2,'Color','r');title('Reference','FontName','Times New Roman','FontSize',12);
%     set(gcf,'Position',[500,500,1200,300]); 
%     cb = colorbar;
%     cb.Layout.Tile = 'east';
%     cb.Label.String = 'Phase(rad)';
%     cb.Label.FontSize = 12;
%     cb.Label.FontName = 'Times New Roman';
%     cb.Limits = [0 1];
%     colormap('parula'); % 使用 jet 颜色映射
%     tlayout.TileSpacing = 'compact';
%     tlayout.Padding = 'compact';
%     saveas(fig,['Data/',experimentName,'/Data (',int2str(g),')/unwarpphase.fig']);
%     saveas(fig, ['Data/',experimentName,'/Data (',int2str(g),')/unwarpphase.bmp'], 'bmp');
%     saveas(fig, ['Data/',experimentName,'/Data (',int2str(g),')/unwarpphase.jpg'], 'jpg');
end
% ========================== TIME 图窗 ==========================
% fig = figure;
% plot(X,TIME(1,:),X,TIME(2,:)+TIME(6,:),'-o',X,TIME(3,:)+TIME(5,:),'-*',X,TIME(4,:),'-.',X,TIME(5,:),':',X,TIME(6,:),'-+','LineWidth',2);
% grid on;
% xlabel(name,'FontSize',12);
% ylabel('Time(s)','FontSize',12);
% set(gca,'FontName','Times New Roman','FontSize',12,'GridAlpha',0.5);
% set(gcf,'Position',[0,0,960,540]);
% legend('LS','Proposed Method','Zhang','WPCA','ZhangEstimationTime','MyEstimationNoise','FontSize',12,'Location','northwest','NumColumns',3);
% saveas(fig,['Data/',experimentName,'/TIME.fig']);
% saveas(fig, ['Data/',experimentName,'/TIME.bmp'], 'bmp');

% TIME_LS = TIME(1,:);
% TIME_WLS = TIME(2,:)+TIME(6,:);
% TIME_WLS_NOISE = TIME(6,:);
% TIME_WLS_ZHANG = TIME(3,:)+TIME(5,:);
% TIME_WLS_ZHANG_NOISE = TIME(5,:);
% TIME_WPCA = TIME(4,:); 
% 
% disp(mean((TIME_WLS - TIME_LS)./TIME_LS));
% disp(mean((TIME_WLS - TIME_WLS_ZHANG)./TIME_WLS_ZHANG));
% disp(mean((TIME_WLS - TIME_WPCA)./TIME_WPCA));
% disp(mean((TIME_WLS_ZHANG_NOISE - TIME_WLS_NOISE)./TIME_WLS_ZHANG_NOISE));

% ========================== RMSE 图窗 ==========================
fig = figure;
plot(X,RMSE(1,:),X,RMSE(2,:),'-o',X,RMSE(3,:),'-*',X,RMSE(4,:),'-+','LineWidth',2);
set(gca,'FontName','Times New Roman','FontSize',11,'GridAlpha',0.5);
grid on;
xlabel(name,'FontSize',15,'FontName','Times New Roman');
ylabel('RMSE (rad)','FontSize',15,'FontName','Times New Roman');
legend('LS','Proposed Method','Zhang','WPCA','FontSize',15,'FontName','Times New Roman');
saveas(fig,['Data/',experimentName,'/RMSE.fig']);
saveas(fig, ['Data/',experimentName,'/RMSE.bmp'], 'bmp');

% fprintf("RMSE[min~max]: \n" + ...
%     "LS[%.3f~%.3f] \n" + ...
%     "WLS[%.3f~%.3f] \n" + ...
%     "Zhang[%.3f~%.3f] \n" + ...
%     "WPCA[%.3f~%.3f] \n", ...
%     min(RMSE(1,:)), max(RMSE(1,:)), ...
%     min(RMSE(2,:)), max(RMSE(2,:)), ...
%     min(RMSE(3,:)), max(RMSE(3,:)), ...
%     min(RMSE(4,:)), max(RMSE(4,:)));
% LS = abs(RMSE(1,:)-RMSE(2,:))./RMSE(1,:);
% WPCA = abs(RMSE(4,:)-RMSE(2,:))./RMSE(4,:);
% Zhang = abs(RMSE(3,:)-RMSE(2,:))./RMSE(3,:);
% fprintf("百分比[min~max]: \n" + ...
%     "LS[%.4f~%.4f] \n" + ...
%     "Zhang[%.4f~%.4f] \n" + ...
%     "WPCA[%.4f~%.4f] \n", ...
%     min(LS), max(LS), ...
%     min(Zhang), max(Zhang), ...
%     min(WPCA), max(WPCA));
% ========================== RMSE-Deviation 图窗 ==========================
% fig = figure;
% plot(B,(RMSE(1,:)-RMSE(2,:))./RMSE(1,:),B,(RMSE(1,:)-RMSE(3,:))./RMSE(1,:),'-*',B,(RMSE(1,:)-RMSE(4,:))./RMSE(1,:),'-o');
% xlabel(name);
% ylabel('Deviation');
% legend('Proposed Method','Zhang','WPCA');
% title("RMSE-Deviation(对比LS)");
% saveas(fig,['Data/',experimentName,'/RMSE-Deviation.fig']);
% saveas(fig, ['Data/',experimentName,'/RMSE-Deviation.bmp'], 'bmp');
%==========================unwarpphase====================================
size = 500;
pixel = linspace(1,size,size);
LS = abs(LS_unwarpphase(size/2,:) - mean(LS_unwarpphase(size/2,:)) - (unwarpphase(size/2,:) - mean(unwarpphase(size/2,:))));
WLS = abs(WLS_unwarpphase(size/2,:) - mean(WLS_unwarpphase(size/2,:)) - (unwarpphase(size/2,:) - mean(unwarpphase(size/2,:))));
WLS_Zhang = abs(WLS_Zhang_unwarpphase(size/2,:) - mean(WLS_Zhang_unwarpphase(size/2,:)) - (unwarpphase(size/2,:) - mean(unwarpphase(size/2,:))));
WPCA = abs(WPCA_unwarpphase(size/2,:) - mean(WPCA_unwarpphase(size/2,:)) - (unwarpphase(size/2,:) - mean(unwarpphase(size/2,:))));

windows = 25; 
LS = movmean(LS,windows);
WLS = movmean(WLS,windows);
WLS_Zhang = movmean(WLS_Zhang,windows);
WPCA = movmean(WPCA,windows);
% disp(mean((LS - WLS)./LS));
% disp(mean((WLS_Zhang - WLS)./LS));
% disp(mean((WPCA - WLS)./LS));

LS_line = mean(movmean(LS,windows));
WLS_line = mean(movmean(WLS,windows));
Zhang_line = mean(movmean(WLS_Zhang,windows));
WPCA_line = mean(movmean(WPCA,windows));

fig  = figure;
plot(pixel,LS,'-.',pixel,WLS,'-*',pixel,WLS_Zhang,'-o',pixel,WPCA,'-+','LineWidth',1);
line([1, size],[LS_line, LS_line],'LineWidth',2,'Color','#0072BD');
line([1, size],[WLS_line, WLS_line],'LineWidth',2,'Color','#D95319');
line([1, size],[Zhang_line, Zhang_line],'LineWidth',2,'Color','#EDB120');
line([1, size],[WPCA_line, WPCA_line],'LineWidth',2,'Color','#7E2F8E');
grid on;
xlabel('Pixel','FontSize',15);
ylabel('Error (rad)','FontSize',15);
set(gca,'FontName','Times New Roman','FontSize',15,'GridAlpha',0.5);
set(gcf,'Position',[500,500,1200,500]);
legendLS = ['LS(',num2str(LS_line),')'];
legendWLS = ['Proposed Method(',num2str(WLS_line),')'];
legendZhang = ['Zhang(',num2str(Zhang_line),')'];
legendWPCA = ['WPCA(',num2str(WPCA_line),')'];
legend(legendLS,legendWLS,legendZhang,legendWPCA);
saveas(fig, ['Data/',experimentName,'/error.fig']);
saveas(fig, ['Data/',experimentName,'/error.bmp'], 'bmp');

